home *** CD-ROM | disk | FTP | other *** search
- /*
- * Stdio Extensions Demo
- * ⌐ 1989, Nigel Perry.
- *
- * There are three extensions to stdio:
- *
- * 1) Addition of %k format to printf to format int/long as chars.
- * This costs 102 bytes of code in printf.
- * 2) Addition of ".SF" 'driver' to fopen() and friends.
- * If the file name is given as ".SF" then fopen() will call
- * standard file to obtain file name, SFGet is used for read or append
- * access, SFPut for write.
- * 3) Addition of fdopen() and fdreopen().
- * Named after their Unix counterparts, these routines take a
- * volume reference number and a PASCAL string to identify the
- * file instead of the fopen/freopen C string.
- *
- * 2) & 3) add 140 bytes to the fopen() code.
- *
- */
-
- #include <stdio+.h>
- #include <FileMgr.h>
- #include <HFS.h>
-
- #define NIL ((void *)0)
-
- OSErr fpath(FILE *, int *, StringPtr);
-
- main()
- { FILE *in, *out;
- int c2;
- long c4;
- char line[80];
- Str255 fname;
- int fvol;
- OSErr err;
- extern int errno;
-
- printf("Stdio Modifications Test/Demo\n\n");
-
- c2 = 'ok';
- c4 = 'test';
-
- fputs("New %k format: printf(\":%3k:%.1k:%-4.3lk:%5lk:\\n\", c2, c2, c4, c4)\n", stdout);
- fputs("Where int c2 = 'ok' and long c4 = 'test': ", stdout);
- printf(":%3k:%.1k:%-4.3lk:%5lk:\n", c2, c2, c4, c4);
-
- fputs("\nTest of new 'driver' \".SF\", first select a file to write to:", stdout);
- fflush(stdout);
-
- out = fopen(".SF", "w");
- fputs("This file created/overwritten by fopen(\".SF\", \"w\")\n", out);
- fclose(out);
-
- fputs("\n\nPlease select file again to append to it:", stdout);
- fflush(stdout);
- out = fopen(".SF", "a");
- fputs("This line added after fopen(\".SF\", \"a\")\n", out);
- fclose(out);
-
- fputs("\n\nNow select again to read back in:", stdout);
- fflush(stdout);
- in = fopen(".SF", "r");
- putchar('\n');
- while( fgets(line, 80, in) != NULL ) fputs(line, stdout);
- (void)fpath(in, &fvol, fname);
- fclose(in);
-
- fputs("\nFinally test fdopen() by rereading file:", stdout);
- in = fdopen(fvol, fname, "r");
- if(in == NULL) return;
- putchar('\n');
- while( fgets(line, 80, in) != NULL ) fputs(line, stdout);
- fclose(in);
-
-
- }
-
- /* HFS routine to work back from FILE * to volref/name */
- OSErr fpath(fl, pvol, pname) FILE *fl; int *pvol; StringPtr pname;
- { FCBPBRec pb;
- OSErr err;
- WDPBRec wb;
-
- pb.ioRefNum = fl->refnum;
- pb.ioCompletion = NIL;
- pb.ioNamePtr = pname;
- pb.ioVRefNum = 0;
- pb.ioFCBIndx = 0;
- if( (err = PBGetFCBInfo(&pb, false)) != noErr) return(err);
-
- wb.ioCompletion = NIL;
- wb.ioNamePtr = NIL;
- wb.ioVRefNum = pb.ioFCBVRefNum;
- wb.ioWDDirID = pb.ioFCBParID;
- wb.ioWDProcID = 'ERIK';
- err = PBOpenWD(&wb, false);
-
- *pvol = wb.ioVRefNum;
-
- return(err);
- }
-